// pages/classHomePage/pk/pkMessage/pkMessage.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    people: null,
    activity_id: null,
    is_fininshed: null,
    stu_meaasge: null,
    exampaper: null,
    exampaper_list: [],
    is_user: 0,
    item: null,
    student_answer_list: [],
    others_1_student_answer_list: [],
    others_2_student_answer_list: [],
    have_winner: 0,
    winner: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    _this.setData({
      people: wx.getStorageSync('people')
    })
    if (options != null) {
      let data = JSON.parse(options['pk'])
      console.log('页面跳转传值：', data)
      let user = 0
      let new_item = null
      return new Promise(function (resolve, reject) {
        new_item = new_item = data['stu_item1'] //初始随机一个
        if (data['student_1_name'] == app.globalData.student_info['name']) {
          new_item = data['stu_item1']
        } else if (data['student_2_name'] == app.globalData.student_info['name']) {
          new_item = data['stu_item2']
        }
        console.log('new_item', new_item);
        if (data['student_1_name'] == wx.getStorageSync('account') || data['student_2_name'] == wx.getStorageSync('account')) {
          //查看是否做过 user = 1:没做 =0： 做过
          user = 1
          // let pageList_data = {
          //   createUser: app.globalData.student_info['id'],
          //   examPaperId: new_item,
          //   pageIndex: 1,
          //   pageSize: 10
          // }
          // app.formPost('/api/admin/student/exampaper/answer/pageList', pageList_data).then(res2 => {
          //   console.log('pageList:', res2);
          //   if (res2['response']['list'].length != 0) {
          //     user = 0
          //   }
          //   console.log(' user user user user', user,res2['response']['list'].length);
          //   resolve()
          // })
          resolve()
        }
        if (wx.getStorageSync('people') == 'teacher') {
          user = 0
        }
        _this.setData({
          activity_id: data['activity_id'],
          stu_meaasge: data,
          is_user: user,
          item: new_item
        })
        resolve()
      }).then(() => {
        _this.get_pk_state()
        _this.get_winner()
        console.log('_this.data.is_user', _this.data.is_user);
        if (_this.data.is_user == 1) {
          _this.get_exampaper()
          _this.read_paper()
        } else if (_this.data.is_user == 0) {
          _this.others_get_exampaper()
          _this.others_read_paper()
        }
      })
    }


  },

  get_pk_state: function () {
    let _this = this
    let data = {}
    return new Promise(function (resolve, reject) {
      if (wx.getStorageSync("people") == 'student') {
        data = {
          'course_name': app.globalData.banji_info['className'],
          'activity_id': _this.data.activity_id,
        }
      } else if (wx.getStorageSync("people") == 'teacher') {
        data = {
          'course_name': app.globalData.banji_info['courseName'],
          'activity_id': _this.data.activity_id,
        }
      }

      app.formPost('/get_pk_state', data).then(res => {
        console.log('获取pk结束状态:', res);
        _this.setData({
          is_fininshed: res['message']
        })
        _this.onLoad()
      })
    })
  },

  get_exampaper: function () {
    let _this = this
    let id = _this.data.item
    let data = {}
    app.formPost('/api/admin/student/exampaper/select/' + String(id), data).then(res => {
      res = JSON.parse(JSON.stringify(res['response']).replace(/<[^>]*>/g, ''))
      console.log('试卷', res);
      _this.setData({
        exampaper: res,
        exampaper_list: res['titleItems'][0]['questionItems']
      })
    })
  },

  read_paper: function () {
    let _this = this
    let id = _this.data.item
    let data = {
      createUser: app.globalData.student_info['id'],
      examPaperId: id,
      pageIndex: 1,
      pageSize: 10
    }
    app.formPost('/api/admin/student/exampaper/answer/pageList', data).then(res => {
      console.log('获取试卷答题记录：', res);
      let paper_id = res['response']['list'][0]['id']
      let new_data = {}
      app.formPost('/api/admin/student/exampaper/answer/read/' + paper_id, new_data).then(res => {
        console.log('获取学生答题结果：', res);
        _this.setData({
          student_answer_list: res['response']['answer']
        })
      })
    })
  },

  others_get_exampaper: function () {
    let _this = this
    let id = _this.data.stu_meaasge['stu_item1']
    let data = {}
    // console.log('hhhhhhhhhhh', _this.data.item);
    app.formPost('/api/admin/student/exampaper/select/' + String(id), data).then(res => {
      res = JSON.parse(JSON.stringify(res['response']).replace(/<[^>]*>/g, ''))
      console.log('试卷', res);
      _this.setData({
        exampaper: res,
        exampaper_list: res['titleItems'][0]['questionItems']
      })
    })
  },

  others_read_paper: function () {
    let _this = this
    console.log('stu_meaasge', _this.data.stu_meaasge);
    let data1 = {
      createUser: _this.data.stu_meaasge['student_id_1'],
      examPaperId: _this.data.stu_meaasge['stu_item1'],
      pageIndex: 1,
      pageSize: 10
    }
    let data2 = {
      createUser: _this.data.stu_meaasge['student_id_2'],
      examPaperId: _this.data.stu_meaasge['stu_item2'],
      pageIndex: 1,
      pageSize: 10
    }
    app.formPost('/api/admin/student/exampaper/answer/pageList', data1).then(res => {
      console.log('获取stu1试卷答题记录：', res);
      if (res['response']['list'].length > 0) {
        let paper_id = res['response']['list'][0]['id']
        let new_data = {}
        app.formPost('/api/admin/student/exampaper/answer/read/' + paper_id, new_data).then(res => {
          console.log('获取学生1答题结果：', res);
          _this.setData({
            others_1_student_answer_list: res['response']['answer']
          })
        })
      }

    })
    app.formPost('/api/admin/student/exampaper/answer/pageList', data2).then(res => {
      console.log('获取stu2试卷答题记录：', res);
      if (res['response']['list'].length > 0) {
        let paper_id = res['response']['list'][0]['id']
        let new_data = {}
        app.formPost('/api/admin/student/exampaper/answer/read/' + paper_id, new_data).then(res => {
          console.log('获取学生2答题结果：', res);
          _this.setData({
            others_2_student_answer_list: res['response']['answer']
          })
        })
      }

    })
  },

  teacher_close_pk: function () {
    let _this = this
    // console.log(e);
    let activity_id = _this.data.activity_id
    console.log('activity_id', activity_id);
    let items = null
    items = String(_this.data.stu_meaasge['stu_item1']) + ',' + String(_this.data.stu_meaasge['stu_item2'])
    let data = {
      'activity_id': activity_id,
      'course_name': app.globalData.banji_info['courseName'],
      'items': items
    }
    app.formPost('/close_pk', data).then(res => {
      console.log('关闭pk结果：', res);
      wx.navigateBack()
    })
  },

  btn_win1: function () {
    let _this = this
    let data = {
      'activity_id': _this.data.activity_id,
      'student_id': _this.data.stu_meaasge['student_id_1']
    }
    app.formPost('/add_pk_winner', data).then(res => {
      console.log('添加学生1为胜者：', res);
    })
  },

  btn_win2: function () {
    let _this = this
    let data = {
      'activity_id': _this.data.activity_id,
      'student_id': _this.data.stu_meaasge['student_id_2']
    }
    app.formPost('/add_pk_winner', data).then(res => {
      console.log('添加学生2为胜者：', res);
    })
  },

  get_winner: function () {
    let _this = this
    let data = {
      'activity_id': _this.data.activity_id
    }
    console.log(' _this.data.activity_id', _this.data.activity_id);
    return new Promise(function (resolve, reject) {
      app.formPost('/get_winner', data).then(res => {
        console.log('查询winner：', res);
        if (res['activity_id'] != '' && res['activity_id'] != null) {
          console.log('没有');
          _this.setData({
            have_winner: 1,
            winner: res
          })
        }
        resolve()
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})